﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManage : MonoBehaviour
{

    const string BgMusic = "Audio/bg/";
    const string SoundMusic = "Audio/sound/";
    public static bool IsonBgMusic = true;
    public static bool IsonSoundMusic = true;
    static AudioSource Bg_Music=null;   
    
    public static void BgPlay(string bgName)
    {
        if(Bg_Music==null)
        {
            Bg_Music = Camera.main.gameObject.AddComponent<AudioSource>();
        }
        Bg_Music.clip = LoadClip(BgMusic+bgName);
        Debug.Log(BgMusic + bgName);
        Bg_Music.loop = true;
        if (IsonBgMusic)
        {
            Bg_Music.Play();
        }
        else
        {
            Bg_Music.Stop();
        }
    }

    static List<AudioSource> Sound_Miusic = new List<AudioSource>();
    public static void SoundPlay(string soundName)
    {
        if(IsonSoundMusic)
        {
            AudioSource sound = null;
            if (Sound_Miusic != null)
            {
                foreach (var s in Sound_Miusic)
                {
                    if (!s.isPlaying)
                    {
                        sound = s;
                    }
                }
            }
            if (sound != null)
            {
                sound.clip = LoadClip(SoundMusic + soundName);
                sound.loop = false;
                sound.Play();
            }
            else
            {
                sound = Camera.main.gameObject.AddComponent<AudioSource>();
                Sound_Miusic.Add(sound);
                sound.clip = LoadClip(SoundMusic + soundName);
                sound.loop = false;
                sound.Play();
            }
        }        
    }
    
    static AudioClip LoadClip(string path)
    {
        return Resources.Load<AudioClip>(path);
    }
}
